<!--
 * @Description:业务系统
-->
<template>
  <div class="registered-container businessRecon appMainWarp">
    <el-form :inline="true" :model="searchKey" class="demo-form-inline">
      <el-form-item>
        <el-button :type="0 == quaflat ? 'primary':''" @click="getlisttable(0)">按天显示</el-button>
        <el-button :type="1 == quaflat ? 'primary':''" @click="getlisttable(1)">按月显示</el-button>
        <el-button :type="2 == quaflat ? 'primary':''" @click="getlisttable(2)">按年显示</el-button>
        <el-button :type="3 == quaflat ? 'primary':''" @click="getlisttable(3)">汇总显示</el-button>
      </el-form-item>
      <el-form-item label="交易时间:">
        <el-date-picker
          v-model="searchKey.time"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
          :clearable="false"
        />
      </el-form-item>
      <el-form-item label="院区:">
        <el-select v-model="searchKey.hospital_area_no" placeholder="请选择">
          <el-option
            v-for="item in searchKey.Hospital"
            :key="item.area_code"
            :label="item.area_name"
            :value="item.area_code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="选择支付场景:">
        <el-select v-model="searchKey.system_no" multiple placeholder="请选择支付场景" clearable>
          <el-option
            v-for="item in searchKey.System"
            :key="item.pay_scene_no"
            :label="item.pay_scene_name"
            :value="item.pay_scene_no"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="对账业务:">
        <el-select v-model="searchKey.reconciliation" placeholder="请选择" @change="onLoad">
          <el-option
            v-for="item in searchKey.reconciliationList"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="onLoad">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-download" @click="onExport">导出</el-button>
      </el-form-item>

      <!-- <div v-if="conf_key== 'BANK_DATA'" class="tips">
        {{ conf_desc }}
      </div> -->
    </el-form>
    <template>
      <el-table
        ref="table"
        v-loading="page.tableLoading"
        v-el-height-adaptive-table="{bottomOffset: 90}"
        center
        max-height="500px"
        class="table"
        :data="searchData"
        :span-method="objectSpanMethodd"
        stripe
        border=""
      >
        <el-table-column label="时间" align="center" prop="time">
          <template slot-scope="scope">{{ scope.row.date ||'-' }}</template>
        </el-table-column>
        <el-table-column label="业务类型" align="center">
          <template slot-scope="scope">{{ scope.row.bill_type ||'-' }}</template>
        </el-table-column>

        <el-table-column
          v-if="searchKey.reconciliation !== '3'"
          :key="1"
          prop="self_total_fee"
          label="支付场景收入"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.self_total_fee||'-' }}</template>
        </el-table-column>
        <el-table-column
          v-if="searchKey.reconciliation !== '2'"
          :key="2"
          prop="his_total_fee"
          label="HIS系统收入"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.his_total_fee	||'-' }}</template>
        </el-table-column>
        <el-table-column
          v-if="searchKey.reconciliation !== '1'"
          :key="3"
          prop="bank_total_fee"
          label="支付系统收入"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.bank_total_fee	||'-' }}</template>
        </el-table-column>
        <el-table-column
          v-if="searchKey.reconciliation == '1' || searchKey.reconciliation == '4'"
          :key="4"
          label="支付场景—HIS系统"
          align="center"
          prop="self_his"
        >
          <template slot-scope="scope">
            <div
              :class="[scope.row.self_his > 0|| scope.row.self_his < 0? 'abnormal':'normal']"
              @click="jumpClick(scope.row,1,scope.row.self_his)"
            >{{ scope.row.self_his ||'0' }}</div>
          </template>
        </el-table-column>

        <el-table-column
          v-if="searchKey.reconciliation == '2' || searchKey.reconciliation == '4'"
          :key="5"
          label="支付场景—支付系统"
          align="center"
          prop="self_bank"
        >
          <template slot-scope="scope">
            <div
              :class="[scope.row.self_bank >0 || scope.row.self_bank <0?'abnormal':'normal']"
              @click="jumpClick(scope.row,2,scope.row.self_bank)"
            >{{ scope.row.self_bank ||'0' }}</div>
          </template>
        </el-table-column>

        <el-table-column
          v-if="searchKey.reconciliation == '3' || searchKey.reconciliation == '4'"
          :key="6"
          label="HIS系统—支付系统"
          align="center"
          prop="his_bank"
        >
          <template slot-scope="scope">
            <div
              :class="[scope.row.his_bank>0 || scope.row.his_bank<0?'abnormal':'normal']"
              @click="jumpClick(scope.row,3,scope.row.his_bank)"
            >{{ scope.row.his_bank||'0' }}</div>
          </template>
        </el-table-column>

      </el-table>
    </template>
    <pagination
      :total="page.totalRow"
      :page.sync="page.currentPage"
      :limit.sync="page.pageSize"
      @pagination="onSearch"
    />
  </div>
</template>

<script>
import elHeightAdaptiveTable from '@/directive/el-table'
import Pagination from '@/components/Pagination' // 分页
import { getConfigType } from '@/api/transactRecords/index'
import { billType } from '@/api/generaLedger/index'
import { getCompareType } from '@/utils/auth'

export default {
  name: 'BusinessRecon',
  components: {
    Pagination
  },
  directives: { elHeightAdaptiveTable },

  data() {
    return {
      quaflat: 0, // 按钮默认
      date: 'day', // 按天月年
      searchKey: {
        time: '', // 交易日期
        hospital_area_no: '', // 选择院区
        Hospital: [],
        system_no: '', // 平台
        System: [],
        reconciliation: '', // 对账业务
        reconciliationList: []
      },

      searchData: [],
      conf_key: '',
      conf_desc: '',
      date_type: '', // 按什么显示
      self_his: '1,2',
      self_bank: '1,3',
      his_bank: '2,3',

      page: {
        tableLoading: false,
        currentPage: 1, // 当前页
        pageSize: 20, // 默认条数
        totalRow: 0, // 总条数
        totalPage: 0 // 总页数
      },
      minDate: '',
      maxDate: '',
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.minDate = minDate
          this.maxDate = maxDate
        },
        disabledDate: (time) => { // 查询时间跨度为365天
          if (this.minDate) {
            var range = 365 * 24 * 3600 * 1000
            return time.getTime() > Date.now() || time.getTime() > (this.minDate.getTime() + range) || time.getTime() < (this.minDate.getTime() - range)
          }
          return time.getTime() > Date.now()
        }
      },
      position: 0, // 合并单元格
      rowSpanArr: [], // 合并单元格
      compare: '', // 对账业务
      system: '' // 平台
    }
  },
  watch: {
    'searchKey.time': {
      deep: true,
      handler(val) {
        if (val == null) {
          this.minDate = null
        }
      }
    }
  },
  created() {
    this.searchKey.time = this.utils.Formonth()
    this.searchKey.reconciliation = getCompareType()
  },
  mounted() {
    this.getConfig()
    this.onLoad()
  },
  methods: {
    // 表格合并
    objectSpanMethodd({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this.rowSpanArr[rowIndex]
        return {
          rowspan: _row, // 行
          colspan: 1 // 列
        }
      }
    },
    // 系统配置搜索
    getConfig() {
      var that = this
      var params = {}
      getConfigType(params)
        .then((res) => {
          that.searchKey.System = res.data.scene
          that.searchKey.Hospital = res.data.hospital
          that.searchKey.reconciliationList = res.data.compare
        })
        .catch(() => {})
    },
    // 查询列表
    onLoad() {
      this.page.currentPage = 1
      this.onSearch()
    },
    onSearch() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.daytype = that.date
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.startime = that.searchKey.time[0]
        params.endtime = that.searchKey.time[1]
      } else {
        params.startime = ''
        params.endtime = ''
      }
      params.hospital = that.searchKey.hospital_area_no
      params.sceneno = that.searchKey.system_no.toString()
      params.bustype = that.searchKey.reconciliation.toString()
      that.page.tableLoading = true
      billType(params)
        .then((res) => {
          that.page.tableLoading = false
          that.searchData = res.data.result
          this.getRowSpan()
          that.page.totalPage = res.data.pages
          that.page.totalRow = parseInt(res.data.count)
        })
        .catch(() => {
          that.page.tableLoading = true
        })
    },
    // 根据字段相同单元格
    getRowSpan() {
      this.rowSpanArr = []
      this.searchData.forEach((item, index) => {
        if (index === 0) {
          this.rowSpanArr.push(1)
          this.position = 0
        } else {
          if (this.searchData[index].date === this.searchData[index - 1].date) {
            this.rowSpanArr[this.position] += 1 // 项目名称相同，合并到同一个数组中
            this.rowSpanArr.push(0)
            this.searchData[index].date = this.searchData[index - 1].date
          } else {
            this.rowSpanArr.push(1)
            this.position = index
          }
        }
      })
    },
    getlisttable(flat) {
      this.quaflat = flat
      if (this.quaflat === 0) {
        this.date = 'day'
        this.onLoad()
      } else if (this.quaflat === 1) {
        this.date = 'month'
        this.onLoad()
      } else if (this.quaflat === 2) {
        this.date = 'year'
        this.onLoad()
      } else {
        this.date = 'all'
        this.onLoad()
      }
    },
    // 导出
    onExport() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.daytype = that.date
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.startime = that.searchKey.time[0]
        params.endtime = that.searchKey.time[1]
      } else {
        params.startime = ''
        params.endtime = ''
      }
      params.hospital = that.searchKey.hospital_area_no
      params.sceneno = that.searchKey.system_no.toString()
      params.bustype = that.searchKey.reconciliation.toString()
      var viewUrl = `/cms/billCompare?&page=${params.page}&limit=${params.limit}&daytype=${
        params.daytype
      }&hospital=${
        params.hospital
      }&startime=${
        params.startime
      }&endtime=${params.endtime}&sceneno=${params.sceneno}&bustype=${params.bustype}&export=${1}`
      window.location.href = process.env.VUE_APP_BASE_API + viewUrl
    },
    jumpClick(item, type, val) {
      this.$router.push({
        path: '/generaLedger/totalRevenue/totalRevenueDetail/index',
        query: {
          billNo: item.bill_no,
          currentime: item.date,
          startime: this.searchKey.time[0],
          endtime: this.searchKey.time[1],
          comparetype: type
        }
      })
      // if (val === 0 || val === '0.00') {
      //   return
      // } else {
      //   this.$router.push({
      //     path: '/generaLedger/totalRevenue/totalRevenueDetail/index',
      //     query: {
      //       date: item.date,
      //       comparetype: type
      //     }
      //   })
      // }
    }
  }
}
</script>

<style lang="scss" scoped>
.businessRecon {
  position: relative;
  // width: 100%;
  .demo-form-inline {
    .el-button--primary {
      background-color: #579ef8 !important;
      color: #fff !important;
    }
    .el-button {
      background-color: #e8f4ff;
      color: #579ef8;
      border: 1px solid rgba(87, 158, 248, 1);
    }
  }
  .abnormal {
    color: red;
    text-decoration: underline;
    cursor: pointer;
  }
  .normal {
    color: rgb(113, 118, 133);
    cursor: pointer;
  }
  .tips{
    color: red;
    font-size: 18px;
    padding: 0 29px 15px;
  }
}
</style>

